<?php

/**
 * @property string $id
 * @property string $uid
 * @property string $wid
 * @property integer $account_type
 * @property string $api_params
 * @property string $account_name
 * @property integer $order_index
 * @property integer $active
 * @author lovebing <lovebing365@126.com> Created on 2015-5-10 
 */
class PayAccountModel extends CActiveRecord {

    const ACCOUNT_TYPE_ALIPAY = 0;
    const ACCOUNT_TYPE_WECHATPAY = 1;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'micro_pay_account';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('uid, wid, api_params, account_name', 'required'),
            array('account_type, order_index, active', 'numerical', 'integerOnly' => true),
            array('uid, wid', 'length', 'max' => 10),
            array('api_params', 'length', 'max' => 2000),
            array('account_name', 'length', 'max' => 100),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, uid, wid, account_type, api_params, account_name, order_index, active', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'uid' => 'Uid',
            'wid' => 'Wid',
            'account_type' => 'Account Type',
            'api_params' => 'Api Params',
            'account_name' => 'Account Name',
            'order_index' => 'Order Index',
            'active' => 'Active',
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return PayAccountModel the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 
     * @param int $comId
     * @param int $accountType
     * @return PayAccountModel
     */
    public static function findModel($comId, $accountType) {
        $c = new CDbCriteria();
        $c->order = 'order_index desc';
        $c->condition = 'wid = :wid and account_type = :account_type and active = 1';
        $c->params = array(
            ':wid' => $comId,
            ':account_type' => $accountType
        );
        return self::model()->find($c);
    }

    /**
     * 
     * @param int $comId
     * @return PayAccountModel
     */
    public static function findAliPayAccount($comId) {
        return self::findModel($comId, self::ACCOUNT_TYPE_ALIPAY);
    }

    /**
     * 
     * @param int $comId
     * @return PayAccountModel
     */
    public static function findWechatPayAccount($comId) {
        return self::findModel($comId, self::ACCOUNT_TYPE_WECHATPAY);
    }
    
    /**
     * 
     * @return array
     */
    public function getApiParams() {
        return json_decode($this->api_params, true);
    }

}
